/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;
import DTO.DONHANGDTO;
import SQL.DataProvider;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
/**
 *
 * @author NGUYEN
 */
public class DONHANGDAO {
    
    public DONHANGDTO timhoadoncoma(int ma)
    {
        DataProvider pro = new DataProvider();
        DONHANGDTO dto = new DONHANGDTO();
        ResultSet rs=null;
        try
        {
            pro.open();
            String query = "SELECT * FROM DonHang";
            rs = pro.excutequery(query);  
            if(rs.next())
            {
                dto.setMadonhang(Integer.parseInt(rs.getString("MaDonHang")));
                dto.setManv(Integer.parseInt(rs.getString("MaNV")));
                dto.setMakh(Integer.parseInt(rs.getString("MaKH")));
                dto.setNgaylap(rs.getDate("NgayLap"));
                dto.setTongtien(Integer.parseInt(rs.getString("TongTien")));  
            }
            return dto;
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }   
        finally
        {
            pro.close();
        }
        return dto;
    }
    
    public ArrayList<DONHANGDTO> laydanhsachhoadon()
    {
        DataProvider pro = new DataProvider();
        ArrayList<DONHANGDTO> ds = new ArrayList<DONHANGDTO>();
        ResultSet rs=null;
        try
        {
            pro.open();
            String query = "SELECT * FROM DonHang";
            rs = pro.excutequery(query);  
            while(rs.next())
            {
                DONHANGDTO dto = new DONHANGDTO();
                
                dto.setMadonhang(Integer.parseInt(rs.getString("MaDonHang")));
                dto.setManv(Integer.parseInt(rs.getString("MaNV")));
                dto.setMakh(Integer.parseInt(rs.getString("MaKH")));
                dto.setNgaylap(rs.getDate("NgayLap"));
                dto.setTongtien(Integer.parseInt(rs.getString("TongTien")));
                
                ds.add(dto);
            }
            return ds;
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }   
        finally
        {
            pro.close();
        }
        return ds;
    }
    
    public boolean themdonhang(DONHANGDTO dto)
    {
        DataProvider pro = new DataProvider();
        int kq;
        try
        {
            pro.open();
            String query = "INSERT INTO DonHang(MaNV,MaKH,NgayLap,TongTien) VALUES("
                    +dto.getManv()+","
                    +dto.getMakh()+",'"
                    +dto.getNgaylap()+"',"
                    +dto.getTongtien()+")";
            kq = pro.excuteupdate(query);
            if(kq>=0)
                return true;
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }   
        return false;
    }
    
    public boolean xoadonhangcoma(int ma)
    {
        DataProvider pro = new DataProvider();
        int kq;
        try
        {
            pro.open();
            String query = "DELETE FROM DonHang WHERE MaDonHang = "+ ma;
            kq = pro.excuteupdate(query);
            if(kq>=0)
                return true;
        }
        catch(Exception e)
        {
            e.printStackTrace();
                    
        }   
        return false;
    }
    
    
    public boolean xoadonhang(DONHANGDTO dto)
    {
        DataProvider pro = new DataProvider();
        int kq;
        try
        {
            pro.open();
            String query = "DELETE FROM DonHang WHERE MaDonHang = "+dto.getManv();
            kq = pro.excuteupdate(query);
            if(kq>=0)
                return true;
        }
        catch(Exception e)
        {
            e.printStackTrace();
                    
        }   
        return false;
    }
    
    
    public boolean xoadonhangcuakhachhangcoma(int ma)
    {
        DataProvider pro = new DataProvider();
        int kq;
        try
        {
            pro.open();
            String query = "DELETE FROM DonHang WHERE MaKH= "+ma;
            kq = pro.excuteupdate(query);
            if(kq>=0)
                return true;
        }
        catch(Exception e)
        {
            e.printStackTrace();         
        }   
        return false;
    }
    public boolean suadonhang(DONHANGDTO dto)
    {
        DataProvider pro = new DataProvider();
        int kq;
        try
        {
            pro.open();
            String query = "UPDATE DonHang set "
                    + "MaNV='"+dto.getManv()
                    +"',MaKH='"+dto.getMakh()
                    +"',NgayLap='"+dto.getNgaylap()
                    +"',TongTien='"+dto.getTongtien()+"' where MaDonHang= "+dto.getMadonhang();
            kq = pro.excuteupdate(query);
            if(kq>=0)
                return true;
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }   
        return false;
    }
}
